Change minimizer¶
This example shows how to change the minimization engine for refinement.
Import¶
import easydiffraction as ed
Matplotlib is building the font cache; this may take a moment.
Job¶
Create a job — the main object to store all the information
job = ed.Job()
Model¶
Load a phase from CIF file
job.add_phase_from_file('data/lbco.cif')
print(job.phases)
Collection of 1 phases: ['lbco']
Show phase info in CIF format
phase = job.phases['lbco']
print(phase.cif)
data_lbco _cell_length_a 3.88 _cell_length_b 3.88 _cell_length_c 3.88 _cell_angle_alpha 90.00000000 _cell_angle_beta 90.00000000 _cell_angle_gamma 90.00000000 _space_group_name_H-M_ref 'P m -3 m' loop_ _atom_site_label _atom_site_type_symbol _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy _atom_site_adp_type _atom_site_B_iso_or_equiv La La 0.00000000 0.00000000 0.00000000 0.5 Biso 0.1 Ba Ba 0.00000000 0.00000000 0.00000000 0.5 Biso 0.1 Co Co 0.5 0.5 0.5 1.00000000 Biso 0.1 O O 0.00000000 0.5 0.5 1.00000000 Biso 0.1
Display the crystal structure of a given model
job.show_crystal_structure(id='lbco')
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
Experiment¶
Load experimentally measured data from a file in XYE format
job.add_experiment_from_file('data/hrpt.xye')
Display the experimentally measured data
job.show_experiment_chart()
Define a point background
background_points = [(10.0, 170),
(165.0, 170)]
job.set_background(background_points)
Display the experiment chart after setting the background
job.show_experiment_chart()
Analysis¶
Display the analysis chart before setting initial parameter values
job.show_analysis_chart()
Create aliases for the two types of experimental parameters
pattern = job.pattern
instrument = job.instrument
Change the default value of the wavelength used in the experiment and display the analysis chart again
instrument.wavelength = 1.494
job.show_analysis_chart()
Select parameters to be refined
phase.scale.free = True
phase.cell.length_a.free = True
pattern.zero_shift.free = True
instrument.resolution_u.free = True
instrument.resolution_v.free = True
instrument.resolution_w.free = True
instrument.resolution_y.free = True
Set the initial values of the parameters to be refined and display the analysis chart again
phase.scale = 7
phase.cell.length_a = 3.88
pattern.zero_shift = 0.3
instrument.resolution_u = 0.1
instrument.resolution_v = -0.1
instrument.resolution_w = 0.2
instrument.resolution_y = 0
job.show_analysis_chart()
Print parameters to be refined (free parameters) before fitting with Lmfit
job.show_free_parameters()
| name | value | error | ||
|---|---|---|---|---|
| 1 | .phases['lbco'].cell.length_a | 3.88 | Å | 0.0 |
| 2 | .phases['lbco'].scale | 7.00 | 0.0 | |
| 3 | .instrument.resolution_u | 0.10 | 0.0 | |
| 4 | .instrument.resolution_v | -0.10 | 0.0 | |
| 5 | .instrument.resolution_w | 0.20 | 0.0 | |
| 6 | .instrument.resolution_y | 0.00 | 0.0 |
Show the current minimization engine
print(job.analysis.current_minimizer)
LMFit_leastsq
Start minimization using the default minimizer (Least Squares method from the Lmfit library)
job.fit()
Fitting successful Duration: 5.19 s Reduced chi: 4.18
Print the refined parameters after fitting with Lmfit
job.show_free_parameters()
| name | value | error | ||
|---|---|---|---|---|
| 1 | .phases['lbco'].cell.length_a | 3.890894 | Å | 0.000068 |
| 2 | .phases['lbco'].scale | 5.988352 | 0.038862 | |
| 3 | .instrument.resolution_u | 0.079206 | 0.005857 | |
| 4 | .instrument.resolution_v | -0.099866 | 0.013233 | |
| 5 | .instrument.resolution_w | 0.096607 | 0.006686 | |
| 6 | .instrument.resolution_y | 0.101565 | 0.003665 | |
| 7 | .pattern.zero_shift | 0.622596 | deg | 0.002247 |
Display the analysis chart after the fitting with Lmfit
job.show_analysis_chart()
Show list of available minimisation engines
print(job.analysis.available_minimizers)
['LMFit', 'LMFit_leastsq', 'LMFit_powell', 'LMFit_cobyla', 'LMFit_differential_evolution', 'LMFit_scipy_least_squares', 'Bumps', 'Bumps_simplex', 'Bumps_newton', 'Bumps_lm', 'DFO', 'DFO_leastsq']
Change the minimization method to simplex from the Bumps library
job.analysis.current_minimizer = 'Bumps_simplex'
print(job.analysis.current_minimizer)
Bumps_simplex
Reset the free parameters to their initial values (before fitting with Lmfit) and display the analysis chart
phase.scale = 7
phase.cell.length_a = 3.88
pattern.zero_shift = 0.3
instrument.resolution_u = 0.1
instrument.resolution_v = -0.1
instrument.resolution_w = 0.2
instrument.resolution_y = 0
job.show_analysis_chart()
Print free parameters before the fitting with Bumps
job.show_free_parameters()
| name | value | error | ||
|---|---|---|---|---|
| 1 | .phases['lbco'].cell.length_a | 3.88 | Å | 0.000068 |
| 2 | .phases['lbco'].scale | 7.00 | 0.038862 | |
| 3 | .instrument.resolution_u | 0.10 | 0.005857 | |
| 4 | .instrument.resolution_v | -0.10 | 0.013233 | |
| 5 | .instrument.resolution_w | 0.20 | 0.006686 | |
| 6 | .instrument.resolution_y | 0.00 | 0.003665 | |
| 7 | .pattern.zero_shift | 0.30 | deg | 0.002247 |
Start minimization with Bumps
job.fit()
Fitting successful Duration: 97.63 s Reduced chi: 4.18
Print free parameters after the fitting with Bumps
job.show_free_parameters()
| name | value | error | ||
|---|---|---|---|---|
| 1 | .phases['lbco'].cell.length_a | 3.890894 | Å | 0.000033 |
| 2 | .phases['lbco'].scale | 5.988349 | 0.019017 | |
| 3 | .instrument.resolution_u | 0.079206 | 0.002866 | |
| 4 | .instrument.resolution_v | -0.099868 | 0.006476 | |
| 5 | .instrument.resolution_w | 0.096610 | 0.003272 | |
| 6 | .instrument.resolution_y | 0.101564 | 0.001793 | |
| 7 | .pattern.zero_shift | 0.622590 | deg | 0.001100 |
Display the analysis chart after the fitting with Bumps
job.show_analysis_chart()